def jwt_response_payload_handler(token, user=None, request=None):
    """
    自定义jwt认证成功返回数据
    """
    return {
        'token': token,
        'user_id': user.id,
        'username': user.username
    }

from django.contrib.auth.backends import ModelBackend
import re
from users.models import User

#定义一个方法 ,根据 正则来匹配手机号,如果不满足手机号就是用户名
def get_user_by_account(username):
    try:
        if re.match(r'1[3-9]\d{9}',username):
            #手机号
            user = User.objects.get(mobile=username)
        else:
            #用户名
            user = User.objects.get(username=username)
    except User.DoesNotExist:
        user=None

    return user


# ModelBackend 为什么继承自它
# 懒 可以少写一个方法
class LoginMobileUsernameModelBackend(ModelBackend):

    """密码
    1.如果用户输入手机号,我们需要通过手机号 查找到该用户
    然后再通过该用户校验密码
    """

    def authenticate(self, request, username=None, password=None, **kwargs):

        user = get_user_by_account(username)

        if user is not None and user.check_password(password):
            return user

        return None


class SettingsBackend(object):
    """
    Authenticate against the settings ADMIN_LOGIN and ADMIN_PASSWORD.

    Use the login name and a hash of the password. For example:

    ADMIN_LOGIN = 'admin'
    ADMIN_PASSWORD = 'pbkdf2_sha256$30000$Vo0VlMnkR4Bk$qEvtdyZRWTcOsCnI/oQ7fVOu1XAURIZYoOZ3iq8Dr4M='
    """

    def authenticate(self, request, username=None, password=None):
        user = get_user_by_account(username)

        if user is not None and user.check_password(password):
            return user
        return None

    def get_user(self, user_id):
        try:
            return User.objects.get(pk=user_id)
        except User.DoesNotExist:
            return None